use axum::Router;
use sea_orm::DatabaseConnection;

pub mod common;
pub mod database;
pub mod enumeration;
pub mod error;
pub mod id;
pub mod latency;
pub mod logger;
pub mod response;
pub mod server;
pub mod yjson;
pub mod ypath;
pub mod yquery;
pub mod yserde;
pub mod yvalid;
pub mod yvalidation;

#[derive(Debug, Clone)]
pub struct AppState {
  pub db: DatabaseConnection,
}

impl AppState {
  pub fn new(db: DatabaseConnection) -> Self {
    Self { db }
  }
}

pub async fn run(router: Router<AppState>) -> anyhow::Result<()> {
  logger::init();
  id::init()?;
  tracing::info!("Starting app server...");

  let db = database::init().await?;
  let state = AppState::new(db);
  let server = server::Server::new(crate::config::get().server());

  server.start(state, router).await
}
